﻿Imports System
Imports System.IO
Imports System.Data.OleDb
Imports System.Data
 

Namespace Arwani
    Partial Class Backend_EditProduct
        Inherits System.Web.UI.Page

        Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
            If Me.txtProductName.Text = "" Then Me.lblError.Text = "Please enter Product name" : Exit Sub
            Dim arwCon As New ArwaniController
            Dim strQ As String
            If CInt(Request.QueryString("pid")) = 0 Then
                strQ = "insert into  product (prodname,proddesc,brandid,catid,sortorder) values ('" & Me.txtProductName.Text & "','" & Me.txtProductDesc.Text & "'," & Me.cboBrand.SelectedItem.Value & ",0," & CInt(Me.txtSortOrder.Text) & ")"
                arwCon.ExecuteNonQuery(strQ)
            Else
                strQ = "update product set sortorder=" & CInt(Me.txtSortOrder.Text) & ", prodname='" & Me.txtProductName.Text & "', proddesc='" & Server.HtmlEncode(Me.txtProductDesc.Text) & "', brandid=" & Me.cboBrand.SelectedItem.Value & " , catid=0  where productid=" & CInt(Request.QueryString("pid"))
                arwCon.ExecuteNonQuery(strQ)
            End If

            Response.Redirect("ManageProducts.aspx")
        End Sub
         
        Protected Sub cmdBack_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdBack.Click
            Response.Redirect("ManageProducts.aspx")
        End Sub

        Public Sub loadBrands()
            Dim arwCon As New ArwaniController
            Dim strQ As String = "select * from brand"
            Dim dtTable As DataTable = arwCon.ExecuteQuery(strQ)
            Me.cboBrand.DataTextField = "brandname"
            Me.cboBrand.DataValueField = "brandid"
            Me.cboBrand.DataSource = dtTable
            Me.cboBrand.DataBind()
        End Sub

        'Public Sub loadCategory()
        '    Dim arwCon As New ArwaniController
        '    Dim strQ As String = "select * from category"
        '    Dim dtTable As DataTable = arwCon.ExecuteQuery(strQ)
        '    Me.cboCategory.DataTextField = "catname"
        '    Me.cboCategory.DataValueField = "catid"
        '    Me.cboCategory.DataSource = dtTable
        '    Me.cboCategory.DataBind()
        'End Sub

        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            If (Session("ADMIN_USER") IsNot Nothing) Then
                If Page.IsPostBack = True Then Exit Sub
                Call loadBrands()
                '  Call loadCategory()
                If Request.QueryString("pid") <> "0" Then
                    Call loadData()
                End If
            Else
                Response.Redirect("Default.aspx")
            End If

        End Sub
        Public Sub loadData()
            Dim arwCon As New ArwaniController
            Dim strQ As String = "select * from product where productid=" & CInt(Request.QueryString("pid"))
            Dim dtTable As DataTable = arwCon.ExecuteQuery(strQ)
            If dtTable.Rows.Count > 0 Then
                Try
                    If Not IsDBNull(dtTable.Rows(0)("prodname")) Then Me.txtProductName.Text = dtTable.Rows(0)("prodname")
                    If Not IsDBNull(dtTable.Rows(0)("sortorder")) Then Me.txtSortOrder.Text = dtTable.Rows(0)("sortorder")
                    If Not IsDBNull(dtTable.Rows(0)("proddesc")) Then Me.txtProductDesc.Text = Server.HtmlDecode(dtTable.Rows(0)("proddesc").ToString().Replace("_x000d_", ""))
                    If Not IsDBNull(dtTable.Rows(0)("brandid")) Then cboBrand.Items.FindByValue(CInt(dtTable.Rows(0)("brandid").ToString())).Selected = True

                    '  If Not IsDBNull(dtTable.Rows(0)("catid")) Then cboCategory.Items.FindByValue(CInt(dtTable.Rows(0)("catid").ToString())).Selected = True
                Catch ex As Exception

                End Try

            End If
        End Sub
    End Class
End Namespace